package com.fer.tankr.models.enemies;

import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.fer.andenginemvc.api.gameObjects.GameObject;
import com.fer.tankr.models.obstacles.Crate;

/**
 * Abstract enemy, when he hits a crate changes direction
 * @author mono
 *
 */
public abstract class ChangeDirectionEnemy extends Enemy {

	public final static int DIRECTION_1 = 1;
	public final static int DIRECTION_2 = 2;
	int direction;
	long deltaChange;
	
	public ChangeDirectionEnemy(PhysicsWorld world, VertexBufferObjectManager vbom,
			float x, float y) {
		super(world, vbom, x, y, 25, 25, 3);
		direction = 1;
		body.setAngularDamping(1f);
		deltaChange = System.currentTimeMillis();
		connector.setUpdateRotation(false);
	}


	
	@Override
	public void onUpdate(float pSecondsElapsed) {
		body.setAngularVelocity(0);
		if (direction == DIRECTION_1)
		{
			onDirection1();
		}
		else if (direction == DIRECTION_2)
		{
			onDirection2();
		}
		
	}
	
	public abstract void onDirection2();

	public abstract void onDirection1();

	@Override
	public void onCollision(GameObject<?> go) {
		super.onCollision(go);
		if(go instanceof Crate)
		{
			if (System.currentTimeMillis()-deltaChange > 200)
			{
				if (direction == DIRECTION_2)
				{
					entity.setRotation(entity.getRotation()+180);
					direction = DIRECTION_1;
				}
				else if (direction == DIRECTION_1)
				{
					entity.setRotation(entity.getRotation()-180);
					direction = DIRECTION_2;
				}
				deltaChange = System.currentTimeMillis();
			}
		}
	}

}
